const Controller = require('egg').Controller;


class wxUser extends Controller {
    async login(){
        const {app,ctx,service} = this;
        const param = ctx.request.body;

        //创建验证规则
        const rule = {
            password:'required',
            phone:{regexp:/^(1[0-9][0-9]|13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/},
            type:'required'
        };
        //验证 验证成功 err为undefined 失败则为错误信息
        const err = await app.validator.validate(param,rule);
        //判断err是否为空
        if(!app.lodash.isEmpty(err)){
            //非空说明 验证失败,抛出401错误,即缺失参数或参数不合规范
            ctx.throw(401,err[0].field+' -- '+err[0].message);
        }

        let rs = await service.wxUserService.login(param);

        ctx.body = {
            code : 200,
            data : rs,
            msg : ''
        }
    }

    async register(){
        const {app,ctx,service} = this;
        let param = ctx.request.body;

        //创建验证规则
        const rule = {
            password:'required',
            realname:'required',
            phone:{regexp:/^(1[0-9][0-9]|13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/},
        };
        //验证 验证成功 err为undefined 失败则为错误信息
        const err = await app.validator.validate(param,rule);
        //判断err是否为空
        if(!app.lodash.isEmpty(err)){
            //非空说明 验证失败,抛出401错误,即缺失参数或参数不合规范
            ctx.throw(401,err[0].field+' -- '+err[0].message);
        }

        let rs = await service.wxUserService.register(param);

        //判断新增是否成功
        //此处只会捕捉到因为记录已存在而添加失败的错误 其他错误此处无法捕捉,由最外层的错误捕捉来处理
        if(!rs){
            //失败 只有一个原因,既是记录已存在
            ctx.throw(402,'该手机号已注册!');
        }

        ctx.body = {
            code : 200,
            data : {},
            msg : ''
        }

    }

    async code(){
        const {app,ctx,service} = this;
        const {phone} = ctx.query;
        let num = "";
        for(let i = 0 ; i < 4 ; i++){
            num += Math.floor(Math.random()*10);
        }

        app.sessionStore.set('code_'+phone,num,3*60*1000);

        ctx.body = {
            code : 200,
            data : num,
            msg : ''
        }

    }

    async forgot(){
        const {app,ctx,service} = this;
        let param = ctx.request.body;

        //创建验证规则
        const rule = {
            password:'required',
            code:'required',
            type:'required',
            phone:{regexp:/^(1[0-9][0-9]|13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/},
        };
        //验证 验证成功 err为undefined 失败则为错误信息
        const err = await app.validator.validate(param,rule);
        //判断err是否为空
        if(!app.lodash.isEmpty(err)){
            //非空说明 验证失败,抛出401错误,即缺失参数或参数不合规范
            ctx.throw(401,err[0].field+' -- '+err[0].message);
        }

        if(param.code != await app.sessionStore.get('code_'+param.phone)){
            ctx.throw(401,'验证码不正确或已失效');
        }

        let rs = await service.wxUserService.forgot(param);

        ctx.body = {
            code : 200,
            data : {},
            msg : ''
        }
    }

    async balance(){
        const {app,ctx,service} = this;
        const {id} = ctx.request.query;
        let param = {
            where:{
                id
            },
            attributes:['money']
        }
        let data = await ctx.model.FdUser.findOne(param);
        ctx.body = {
            code : 200,
            data : data,
            msg : ''
        }
    }
}

module.exports = wxUser;
